"""监控相关路由"""

from fastapi import APIRouter, Depends, Path
from app.models.monitor import MonitorData
from app.routers.auth import get_current_user
from app.services.cloud_providers import get_cloud_provider

router = APIRouter()

@router.get("/monitor/{instance_id}", response_model=MonitorData)
async def get_monitor_data(
    instance_id: str = Path(..., description="实例ID"),
    period: int = 3600,
    current_user: dict = Depends(get_current_user)
):
    """获取实例监控数据"""
    
    # 获取云服务提供商
    provider = get_cloud_provider(
        current_user["provider"],
        current_user["access_key"],
        current_user["secret_key"]
    )
    
    # 获取监控数据
    monitor_data = provider.get_monitor_data(instance_id, period)
    
    return monitor_data

@router.get("/monitor/{instance_id}/cpu")
async def get_cpu_metrics(
    instance_id: str = Path(..., description="实例ID"),
    period: int = 3600,
    current_user: dict = Depends(get_current_user)
):
    """获取CPU监控数据"""
    
    provider = get_cloud_provider(
        current_user["provider"],
        current_user["access_key"],
        current_user["secret_key"]
    )
    
    data = provider.get_monitor_data(instance_id, period)
    return {"cpu": data.cpu}

@router.get("/monitor/{instance_id}/memory")
async def get_memory_metrics(
    instance_id: str = Path(..., description="实例ID"),
    period: int = 3600,
    current_user: dict = Depends(get_current_user)
):
    """获取内存监控数据"""
    
    provider = get_cloud_provider(
        current_user["provider"],
        current_user["access_key"],
        current_user["secret_key"]
    )
    
    data = provider.get_monitor_data(instance_id, period)
    return {"memory": data.memory}

@router.get("/monitor/{instance_id}/network")
async def get_network_metrics(
    instance_id: str = Path(..., description="实例ID"),
    period: int = 3600,
    current_user: dict = Depends(get_current_user)
):
    """获取网络监控数据"""
    
    provider = get_cloud_provider(
        current_user["provider"],
        current_user["access_key"],
        current_user["secret_key"]
    )
    
    data = provider.get_monitor_data(instance_id, period)
    return {"network": data.network}
